#include "readthread.h"

#define WAIT 10*1000

readThread::readThread(QTcpSocket * tcpSocket, passData *passwordData, database *data, QObject *parent)
	: QThread(parent), tcpSocket(tcpSocket), passwordData(passwordData), data(data)
{
}

readThread::~readThread()
{

}

void readThread::run()
{
	if (tcpSocket->bytesAvailable() == 0)
		if (!tcpSocket->waitForReadyRead(WAIT))
		{
			exit(0);
		}

	in.setDevice(tcpSocket);
	in.setVersion(QDataStream::Qt_4_6);

	quint16 blockSize=1,code=0;
	QString block;
	if (QAbstractSocket::ConnectedState==tcpSocket->state())
	{
		in >> blockSize;
		while (tcpSocket->bytesAvailable() < blockSize)
			tcpSocket->waitForDisconnected(100);
		in >> code;

		switch (code)
		{
		case 1:
			checkPassword();
			break;
		case 2:
			addRecord();
			break;
		case 3:
			sendListForOperator();
			break;
		case 4:
			sendOldRecord();
			break;
		case 5:
			updateUsers();
			break;
		}
	}

	tcpSocket->waitForBytesWritten();
}

void readThread::checkPassword()
{
	QString password;
	QString username;
	QString versionClient;

	in >> username >> password >> versionClient;

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_6);
	out << (quint16)0;

	if (versionClient!=versionServer)
		out << false;
	else
		out << passwordData->checkPassword(username,password);

	out.device()->seek(0);
	out << (quint16)(block.size() - sizeof(quint16));
	tcpSocket->write(block);
}

void readThread::addRecord()
{
	int count;
	QString buf;
	in >> data->tmpRecord;

	data->saveTmpRecord();

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_6);
	out << (quint16)0;

	out << data->tmpRecord;
	
	out.device()->seek(0);
	out << (quint16)(block.size() - sizeof(quint16));
	tcpSocket->write(block);
}

void readThread::sendListForOperator()
{
	int author;
	QString buf,date;
	QList <QString> list;

	in >> author >> date;
	data->loadOldData(date);
	for (int i=0; i<data->oldData.count(); i++)
	{
		if (data->oldData[i].vipiskaItem.FIOOperator==author)
		{
			buf.clear();
			buf.append(data->oldData[i].getNumber());
			buf.append(data->oldData[i].vipiskaItem.time.toString("  HH:mm  "));
			buf.append(data->oldData[i].vipiskaItem.date);
			buf.append(" ");
			buf.append(data->oldData[i].vipiskaItem.FIOClient);
			list.append(buf);
		}
	}

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_6);
	out << (quint16)0;

	out << list.count();
	for (int i=0; i<list.count(); i++)
		out << list[i];

	out.device()->seek(0);
	out << (quint16)(block.size() - sizeof(quint16));
	tcpSocket->write(block);
}

void readThread::sendOldRecord()
{
	int number;

	in >> number;

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_6);
	out << (quint16)0;

	for (int i=0; i<data->oldData.count(); i++)
		if (data->oldData[i].getNumber().toInt()==number)
			out << data->oldData[i];

	out.device()->seek(0);
	out << (quint16)(block.size() - sizeof(quint16));
	tcpSocket->write(block);
}

void readThread::updateUsers()
{
	QString versionClientUsersList;
	QString versionClient;
	int count,flag=0;

	in >> versionClientUsersList >> versionClient;

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_6);
	out << (quint16)0;

	if (versionClient!=versionServer)
	{
		flag=2;
		out << flag;
	}
	else
	{
		if (versionClientUsersList!=passwordData->versionUsersList)
		{
			flag=1;
			QList <QString> list=passwordData->getUsersList();
			count=list.count();
			out << flag << passwordData->versionUsersList << count;
			for (int i=0; i< count; i++)
				out << list[i];
		}
		else
			out << flag;
	}
	
	out.device()->seek(0);
	out << (quint16)(block.size() - sizeof(quint16));
	tcpSocket->write(block);
}